package com.drop.action;

import com.drop.common.MyCart;
import com.drop.common.States;
import com.drop.common.Util;
import com.drop.entity.Order;
import com.drop.service.OrderService;
import com.drop.service.OrderShowService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 订单表现层
 * Created by 陈东一 on 2017/9/24 16:17
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/order")
@Scope("prototype")
public class OrderAction extends ActionSupport{

	private Log log = LogFactory.getLog(this.getClass());

	@Autowired
	private OrderService orderService;

	@Autowired
	private OrderShowService ors;

	/**
	 * 插入订单
	 * @return
	 * @throws Exception
	 */
	@Action(value = "add",results = {})
	public String addOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String[] goodsId = request.getParameterValues("goodsId");
		String[] isselect = request.getParameterValues("isselect");
		String[] quantity = request.getParameterValues("quantity");
		int uid = (int) request.getSession().getAttribute("id");
		int id = orderService.addOrder(uid, goodsId, isselect, quantity);
		Util.returnJson(response,new MyCart("true",id+""));
		return null;
	}

	/**
	 * 显示订单
	 * @return
	 * @throws Exception
	 */
	@Action(value = "show",results = {
			@Result(name="pay",location = "/WEB-INF/home/pay.jsp")
	})
	public String showOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String orderId = request.getParameter("orderId");
		Order order = orderService.showOrder(Integer.parseInt(orderId));
		request.setAttribute("order", order);
		return "pay";
	}

	/**
	 * 订单付款
	 * @return
	 * @throws Exception
	 */
	@Action(value = "pay",results = {
			@Result(name="paysuccess",location = "/WEB-INF/home/paysuccess.jsp")
	})
	public String payOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String orderId = request.getParameter("orderId");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String street = request.getParameter("street");
		String address = province+","+city+","+street;
		Order order = orderService.updateOrder(Integer.parseInt(orderId), States.待出库.getValue(), address);
		request.setAttribute("order", order);
		return "paysuccess";
	}

	/**
	 * 显示订单列表
	 * @return
	 * @throws IOException
	 */
	@Action(value = "list",results = {})
	public String showAllGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String type = request.getParameter("type");
		Util.returnJson(response, orderService.showAllOrder(1,page,rows));
		return null;
	}


	/**
	 * 发货
	 * @return
	 * @throws Exception
	 */
	@Action(value = "send",results = {})
	public String addGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String ids = request.getParameter("ids");
		Util.returnJson(response, orderService.updateOrder(ids, States.待签收.getValue()));
		return null;
	}

	/**
	 * 修改订单
	 * @return
	 * @throws Exception
	 */
	@Action(value = "update",results = {})
	public String updateGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String orderId = request.getParameter("orderId");
		String address = request.getParameter("address");
		Util.returnJson(response, orderService.updateOrder(orderId,address));
		return null;
	}

	/**
	 * 取消订单
	 * @return
	 * @throws Exception
	 */
	@Action(value = "delete",results = {})
	public String deleteGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String orderId = request.getParameter("orderId");
		Util.returnJson(response, orderService.deleteOrder(orderId));
		return null;
	}



	@Action(value = "shorder",results = {
			@Result(name="showorder",location = "/WEB-INF/person/order.jsp")
	})
	public String showOrder1(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		//User u=(User) session.getAttribute("user");
		//int userid=u.getUserId();
		int userid = (int) session.getAttribute("id");
		List<Order> all=ors.getAllorder(userid);
		List<Order> wpay=ors.getOrder(userid, 1);
		List<Order> wsend=ors.getOrder(userid, 2);
		List<Order> wreci=ors.getOrder(userid, 3);
		List<Order> wcom=ors.getOrder(userid, 4);
		request.setAttribute("wpay", wpay);
		request.setAttribute("all", all);
		request.setAttribute("wsend", wsend);
		request.setAttribute("wreci", wreci);
		request.setAttribute("wpcom", wcom);
		return "showorder";
	}


	/**
	 * 签收
	 * @return
	 */
	@Action(value = "sign",results = {
			@Result(name="success",location = "shorder" ,type = "chain")
	})
	public String getGoods() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String orderid = (String) request.getParameter("orderId");
		orderService.updateOrder(orderid, States.待评价.getValue());
		return "success";
	}



}
